package com.bang.study.jvm.demo01;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @ClassName: MetaSpaceOom
 * @author: BANG
 * @Date: 2019/10/30 16:38
 * @Version: 1.0
 * @Modified By:
 * @Desc: 元空间内存溢出分析
 */
public class MetaSpaceOom {

    private static long count = 0;

    /**
     * JVM参数
     * -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:MetaspaceSize=10m -XX:MaxMetaspaceSize=10m -XX:+PrintGCDetails -Xloggc:meta-gc.log -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./
     * @param args
     */
    public static void main(String[] args) {
        while (true) {
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(Car.class);
            enhancer.setUseCache(false);
            enhancer.setCallback(new MethodInterceptor() {
                @Override
                public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                    System.out.println("汽车加油");
                    Object target = methodProxy.invokeSuper(o, objects);
                    System.out.println("汽车奔跑");
                    return target;
                }
            });

            Car car = (Car) enhancer.create();
            car.run();
            System.out.println(String.format("创建了%s个Car的子类", ++count));
        }
    }
}


class Car {

    public void run() {
        System.out.println("汽车启动");
    }
}
